home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / unix.subproj / uid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  3.3 KB  |  117 lines

  1. /* uid.c
  2.    Switch back and forth between UUCP and user permissions.
  3.  
  4.    Copyright (C) 1992, 1995 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #include "uudefs.h"
  29. #include "sysdep.h"
  30.  
  31. #include <errno.h>
  32.  
  33. /* NetBSD apparently does not support setuid as required by POSIX when
  34.    using saved setuid, so use seteuid instead.  */
  35.  
  36. #if HAVE_SETEUID
  37. #define setuid seteuid
  38. #endif
  39.  
  40. /* Switch to permissions of the invoking user.  */
  41.  
  42. boolean
  43. fsuser_perms (pieuid)
  44.      uid_t *pieuid;
  45. {
  46.   uid_t ieuid, iuid;
  47.  
  48.   ieuid = geteuid ();
  49.   iuid = getuid ();
  50.   if (pieuid != NULL)
  51.     *pieuid = ieuid;
  52.  
  53. #if HAVE_SETREUID
  54.   /* Swap the effective user id and the real user id.  We can then
  55.      swap them back again when we want to return to the uucp user's
  56.      permissions.  */
  57.   if (setreuid (ieuid, iuid) < 0)
  58.     {
  59.       ulog (LOG_ERROR, "setreuid (%ld, %ld): %s",
  60.         (long) ieuid, (long) iuid, strerror (errno));
  61.       return FALSE;
  62.     }
  63. #else /* ! HAVE_SETREUID */
  64. #if HAVE_SAVED_SETUID
  65.   /* Set the effective user id to the real user id.  Since the
  66.      effective user id is saved (it's the saved setuid) we will able
  67.      to set back to it later.  If the real user id is root we will not
  68.      be able to switch back and forth, so don't even try.  */
  69.   if (iuid != 0)
  70.     {
  71.       if (setuid (iuid) < 0)
  72.     {
  73.       ulog (LOG_ERROR, "setuid (%ld): %s", (long) iuid, strerror (errno));
  74.       return FALSE;
  75.     }
  76.     }
  77. #else /* ! HAVE_SAVED_SETUID */
  78.   /* There's no way to switch between real permissions and effective
  79.      permissions.  Just try to open the file with the uucp
  80.      permissions.  */
  81. #endif /* ! HAVE_SAVED_SETUID */
  82. #endif /* ! HAVE_SETREUID */
  83.  
  84.   return TRUE;
  85. }
  86.  
  87. /* Restore the uucp permissions.  */
  88.  
  89. /*ARGSUSED*/
  90. boolean
  91. fsuucp_perms (ieuid)
  92.      long ieuid;
  93. {
  94. #if HAVE_SETREUID
  95.   /* Swap effective and real user id's back to what they were.  */
  96.   if (! fsuser_perms ((uid_t *) NULL))
  97.     return FALSE;
  98. #else /* ! HAVE_SETREUID */
  99. #if HAVE_SAVED_SETUID
  100.   /* Set ourselves back to our original effective user id.  */
  101.   if (setuid ((uid_t) ieuid) < 0)
  102.     {
  103.       ulog (LOG_ERROR, "setuid (%ld): %s", (long) ieuid, strerror (errno));
  104.       /* Is this error message helpful or confusing?  */
  105.       if (errno == EPERM)
  106.     ulog (LOG_ERROR,
  107.           "Probably HAVE_SAVED_SETUID in policy.h should be set to 0");
  108.       return FALSE;
  109.     }
  110. #else /* ! HAVE_SAVED_SETUID */
  111.   /* We didn't switch, no need to switch back.  */
  112. #endif /* ! HAVE_SAVED_SETUID */
  113. #endif /* ! HAVE_SETREUID */
  114.  
  115.   return TRUE;
  116. }
  117.